# __author:wanghao
# data:2022/6/7
import time

from case.base.basepage import BasePage
from case.base import route

"""
首页小店页面
"""
class StorePage(BasePage):
    """小程序小店页面公共方法"""

    locators = {
        "BASE_ELEMENT": "view",
        "BASE_STORE_IMAGE": "view[catch:tap='gotoShopHomePage'][class='item']",
        "BASE_CHAT_CONTACT": "button.btn.contact-btn.van-button.button-index--van-button.van-button--default",
        "BASE_SHARE_BUTTON": "button[class='btn share-btn']"
    }

    """
    校验页面的小店入口
    """

    def check_homepage_store_element(self):
        # 校验页面是否包含view元素
        self.mini.assertTrue(self.mini.page.element_is_exists(StorePage.locators['BASE_ELEMENT']))
        # 校验页面小店入口
        self.mini.assertTrue(self.mini.page.element_is_exists(StorePage.locators['BASE_STORE_IMAGE']))
        return self

    """
    进入小店校验小店名称
    """

    def check_into_store_page(self):
        # 校验页面是否包含view元素
        self.mini.page.wait_for("view.content-index--shop-name")
        # 获取小店名称
        store_name = self.mini.page.get_element("view.content-index--shop-name").inner_text
        # 进入小店
        self.mini.page.get_element("view.content-index--shop-name").click()
        time.sleep(3)
        into_store_name = self.mini.page.get_element("view.shop-name.certification").inner_text
        self.mini.assertEqual(store_name, into_store_name, "小店名称不符合预期")
        return self

    """
    进入小店其他tab
    """

    def into_test_tab(self):
        # 进入小店
        self.mini.page.get_element("view[class='group-name'][data-index='1']").click()
        return self

    """
    进入商详校验小店商品
    """

    def check_store_good_element(self):
        # 获取商品名称
        element = self.mini.page.get_element(
            "goods-item[is='packages/shop/components/max-goods-item/index'] > view > view")
        goods_name = element.inner_text
        # 进入商详页
        element.click()
        self.mini.page.wait_for("view.title-index--title.van-multi-ellipsis--l2")
        # 获取商详页商品名称
        time.sleep(3)
        detail_goods_name = self.mini.page.get_element(
            "view.title-index--title.van-multi-ellipsis--l2").inner_text
        self.mini.assertIn(goods_name, detail_goods_name, "商品标题不符合预期")
        return self

    """
    进入商详校验分享功能
    """

    def to_store_share_good(self):
        time.sleep(2)
        # 获取商品名称
        element = self.mini.page.get_elements(
            "view.goods-item-index--goods-name.van-multi-ellipsis--l2")[0]
        # 进入商详页
        element.click()
        self.mini.page.wait_for("view.goods-info__title.van-multi-ellipsis--l2")
        # 分享按键
        share_element = self.mini.page.get_element("button.btn.share-btn")
        share_element.click()
        # 海报
        self.mini.assertTrue(self.mini.page.element_is_exists("view.poster-index--share-post-wrapper", 5))
        self.check_store_share_elements()

    """
    校验商详校验分享功能
    """

    def check_store_share_elements(self):
        # 获取分享功能点
        share_elements = self.mini.page.get_elements("view.share-popup-index--share-button__label")
        self.mini.assertEqual(share_elements[0].inner_text, "微信分享", "获取微信分享失败")
        self.mini.assertEqual(share_elements[1].inner_text, "保存海报", "获取保存海报失败")
        self.mini.assertEqual(share_elements[2].inner_text, "复制链接", "获取复制链接失败")
        return self

    """
    进入小店商品商详页
    """
    def to_store_good_detail_page(self):
        # check进入小店页面
        StorePage.check_into_store_page(self)
        StorePage.into_test_tab(self)
        StorePage.check_store_good_element(self)

    """
   进入小店商品分享
   """

    def to_store_good_share_page(self):
        StorePage.check_into_store_page(self)
        StorePage.into_test_tab(self)
        StorePage.to_store_share_good(self)

    """
    校验小店客服入口
    """

    def check_store_chat_contact_btn(self):
        self.check_into_store_page()
        self.mini.assertTrue(self.mini.page.element_is_exists("view.contact-btn"))
        return self

    """
    校验小店分享入口
    """

    def check_store_share_btn(self):
        self.check_into_store_page()
        self.mini.assertTrue(self.mini.page.element_is_exists(StorePage.locators['BASE_SHARE_BUTTON']))
        return self

    """
    购买提交
    """

    def submit_goods_buy(self):
        time.sleep(2)
        self.mini.page.wait_for("view.bar-index--submit-bar__btn")
        buy_submit_button = self.mini.page.get_element("view.bar-index--submit-bar__btn")
        """
        buy_submit_button.click()
        # 微信支付
        self.mini.page.wait_for("view.zan-pay__method.method-index--zan-pay__method.zan-pay__method--hairline")
        wechat_pay_button = self.mini.page.get_element(
            "view.zan-pay__method.method-index--zan-pay__method.zan-pay__method--hairline")
        wechat_pay_button.click()
        return self
        """
    """
    商详页购买商品
    """

    def check_shopping_flow(self):
        # 进入商详页
        # self.to_store_good_detail_page()
        # 商详页发起购买
        buy_button = self.mini.page.get_element("button.components-action-index--e-core-btn.components-action-index--e-core-btn__main")
        buy_button.click()
        # sku选择面板
        sku_flag = self.mini.page.wait_for("view.item-index--van-sku-row__item.attr-item-index--sku-item__root", 1)
        if sku_flag:
            sku_btns = self.mini.page.get_elements("view.item-index--van-sku-row__item.attr-item-index--sku-item__root")
            sku_btns[0].click()
        # 购买确认页面
        self.mini.page.wait_for("button.operation-index-e-core-btn.operation-index--e-core-btn__single")
        buy_sure_button = self.mini.page.get_element("button.operation-index--e-core-btn.operation-index--e-core-btn__single")
        # 极速下单模式下，不需要再点击立即购买在ide模式
        # buy_sure_button.click()
        # # 提交
        # self.submit_goods_buy()
        return self





